package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	// 构建请求体数据
	data := map[string]interface{}{
		"jsonrpc": "2.0",
		"method":  "eth_blockNumber",
		"params":  []interface{}{},
		"id":      1,
	}
	payload, err := json.Marshal(data)
	if err != nil {
		fmt.Println("构建请求体失败:", err)
		return
	}

	// 发送POST请求
	url := "https://mainnet.infura.io/v3/9d36cf2e13b0480aa8285bf4744e4f98"
	resp, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
	if err != nil {
		fmt.Println("发送请求失败:", err)
		return
	}
	defer resp.Body.Close()

	// 读取响应结果
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("读取响应失败:", err)
		return
	}

	// 解析响应结果
	var result map[string]interface{}
	err = json.Unmarshal(respBody, &result)
	if err != nil {
		fmt.Println("解析响应失败:", err)
		return
	}

	// 获取blockNumber
	blockNumber, ok := result["result"].(string)
	if !ok {
		fmt.Println("解析blockNumber失败")
		return
	}

	fmt.Println("当前区块号:", blockNumber)
}
